# 定义路由函数，处理HTTP请求
# 一个全栈项目，不分离前后端

# flask 导入
from flask import Flask, render_template, request, redirect, url_for, session
import utils
from domain import patient, doctor
from domain.doctor import Doctor
from domain.medicalRecord import MedicalRecord
from domain.patient import Patient


class Routes:
    def __init__(self, app):
        self.app = app
        self.register_routes()
        self.app.secret_key = 'secret'

    def register_routes(self):
        # 首页
        @self.app.route('/')
        def index():
            print("首页被访问")
            return render_template('index.html')

        # 患者登录页
        @self.app.route('/patientLogin', methods=['GET', 'POST'])
        def patientLogin():
            print("患者登录页被访问")
            if request.method == 'GET':
                return render_template('patientLogin.html')
            elif request.method == 'POST':
                # 获取表单数据
                patient_card_id = request.form.get('patientCardId')
                patient_password = request.form.get('patientPassword')
                # 调用数据库查询验证输入是否正确
                isPatient = utils.Patient_Login(patient_card_id, patient_password)
                if isPatient:
                    print('登录成功进入患者主页')
                    return redirect('/patientMain')
                else:
                    print('登录失败')
                    return render_template('patientLogin.html')

        # 患者注册页
        @self.app.route('/patientRegister', methods=['GET', 'POST'])
        def patientRegister():
            print("患者注册页被访问")
            if request.method == 'GET':
                return render_template('patientRegister.html')
            elif request.method == 'POST':
                # 获取表单数据
                patient_card_id = request.form.get('patientCardId')
                patient_name = request.form.get('patientName')
                patient_age = request.form.get('patientAge')
                patient_gender = request.form.get('patientGender')
                patient_phone = request.form.get('patientPhone')
                patient_password = request.form.get('patientPassword')
                pat = Patient(patient_card_id, patient_name, patient_age, patient_gender, patient_phone,
                              patient_password)
                if utils.Patient_Register(pat):
                    print('注册成功')
                    return redirect('/patientLogin')
                else:
                    print('注册失败')
                    return redirect('/patientRegister')

        # 医生登录页
        @self.app.route('/doctorLogin', methods=['GET', 'POST'])
        def doctorLogin():
            print("医生登录页被访问")
            if request.method == 'GET':
                return render_template('doctorLogin.html')
            elif request.method == 'POST':
                # 获取表单数据
                doctor_id = request.form.get('doctorId')
                doctor_name = request.form.get('doctorName')
                # 调用数据库查询验证是否为医生
                isDoctor = utils.Doctor_Login(request.form.get('doctorId'), request.form.get('doctorPassword'))
                if isDoctor:
                    print('登录成功进入病历管理页面')
                    # 将用户名保存在session中, 方便后续使用
                    session['doctor_id'] = doctor_id
                    session['doctor_name'] = doctor_name
                    return redirect('/doctorMain')
                else:
                    return render_template('doctorLogin.html')

        # 患者主页面
        @self.app.route('/patientMain')
        def patientMain():
            print("患者主页被访问")
            patient_card_id = session.get('patient_card_id')
            patient_name = session.get('patient_name')
            return render_template('patientMain.html', patient_card_id=patient_card_id, patient_name=patient_name)

        # 医生主页面
        @self.app.route('/doctorMain')
        def manage():
            print("病历管理页面被访问")
            doctor_id = session.get('doctor_id')
            doctor_name = session.get('doctor_name')
            return render_template('doctorMain.html', doctor_name=doctor_name, doctor_id=doctor_id)

        # 诊断页面
        @self.app.route('/diagnosis', methods=['GET', 'POST'])
        def diagnosis():
            print("诊断页被访问")
            doctor_id = session.get('doctor_id')
            doctor_name = session.get('doctor_name')
            if request.method == 'POST':
                # 获取表单数据
                medicalRecord = request.form.get('patient_card_id')
                patient_name = request.form.get('patient_name')
                patient_age = request.form.get('patient_age')
                patient_gender = request.form.get('patient_gender')
                diagnosis_info = request.form.get('diagnosis_info')
                doctor_advice = request.form.get('doctor_advice')
                # 创建病历对象,存放获取的表单数据
                doc = Doctor(doctor_id, doctor_name, '', '', '', '')
                pat = Patient(medicalRecord, patient_name, patient_age, patient_gender, '')
                medicalRecord = MedicalRecord(0, pat, doc, diagnosis_info, doctor_advice)
                # 添加到数据库
                utils.MedicalRecord_Add(medicalRecord)
            return render_template('diagnosis.html', doctor_name=doctor_name, doctor_id=doctor_id)

        # 全部患者信息页
        @self.app.route('/patientsInfo')
        def patientsInfo():
            print("全部患者信息页被访问")
            # 查询全部患者信息
            patients = utils.Patient_Query_All()
            doctor_id = session.get('doctor_id')
            doctor_name = session.get('doctor_name')
            return render_template('patientsInfo.html', patients=patients, doctor_name=doctor_name, doctor_id=doctor_id)

        # 增加患者信息
        @self.app.route('/patientAdd', methods=['GET', 'POST'])
        def patientAdd():
            print("增加患者信息页被访问")
            doctor_id = session.get('doctor_id')
            doctor_name = session.get('doctor_name')

            if request.method == 'POST':
                # 获取表单数据
                card_id = request.form.get('patient_card_id')
                name = request.form.get('patient_name')
                age = request.form.get('patient_age')
                gender = request.form.get('patient_gender')
                phone = request.form.get('patient_phone')
                # 创建患者对象,存放获取的表单数据
                pat = Patient(card_id, name, age, gender, phone)
                print(pat.card_id, pat.name, pat.age, pat.gender, pat.phone)
                try:
                    # 调用数据库插入数据
                    utils.Patient_Add(pat)
                except Exception as e:
                    print(f"Error: unable to insert data. Details: {e}")
            return render_template('patientAdd.html', doctor_name=doctor_name, doctor_id=doctor_id)

        # 病历管理
        @self.app.route('/recordManage')
        def recordManage():
            print("病历管理页被访问")
            doctor_id = session.get('doctor_id')
            doctor_name = session.get('doctor_name')
            # 查询全部病历信息
            records = utils.MedicalRecord_Query_All()
            return render_template('recordManage.html', records=records, doctor_name=doctor_name, doctor_id=doctor_id)
